/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package session;


import javax.ejb.EJB;
import entity.MHotel;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.math.BigDecimal;
/**
 *
 * @author Munliga
 */
@Stateless
public class MHotelFacade extends AbstractFacade<MHotel> {
    @PersistenceContext(unitName = "MedicalHub-warPU")
    private EntityManager em;
     @EJB
    private MHotelFacade mHotelFacade;
    protected EntityManager getEntityManager() {
        return em;
    }

    public MHotelFacade() {
        super(MHotel.class);
    }
public List<MHotel> LoadDataHotel( String txtName, String txtLocation, String txtHotelLvl,String sortIndex, String sortDirection) {
        try {
            String strQuery = "SELECT m FROM MHotel m ";
            String strWhere = " Where 1=1 ";
            strWhere += (!txtName.isEmpty()) ? " AND m.name Like '%" + txtName + "%' " : " ";
            strWhere += (!txtLocation.isEmpty()) ? " AND m.location Like '%" + txtLocation + "%' " : " ";
            strWhere += (!txtHotelLvl.isEmpty()) ? " AND m.hotelLevel Like '%" + txtHotelLvl + "%' " : " ";
            strWhere += "ORDER BY m." + sortIndex + " " + sortDirection;
            strQuery += strWhere;
            return em.createQuery(strQuery).getResultList();
        } catch (Exception e) {
            return null;
        }
    }
  public List<MHotel> findByHotelId(String id) {
        return em.createNamedQuery("MHotel.findById").setParameter("id", Long.parseLong(id)).getResultList();
    }

    public void EditHotel(String id, String txtName, String txtLocation, String txtHotelLvl, String txtLatitude, String txtLongtitude){
        MHotel editHotel = mHotelFacade.find(Long.parseLong(id));
        editHotel.setName(txtName);
        editHotel.setLocation(txtLocation);
        editHotel.setHotelLevel(txtHotelLvl);
        if (!txtLatitude.isEmpty()){
            editHotel.setLatitude(new BigDecimal(txtLatitude));
        }else{
            editHotel.setLatitude(null);
        }
        if (!txtLongtitude.isEmpty()){
            editHotel.setLongtitude(new BigDecimal(txtLongtitude));
        }else{
            editHotel.setLongtitude(null);
        }
        em.persist(editHotel);
    }

    public void DeleteHotel(String id){
        MHotel deleteHotel = mHotelFacade.find(Long.parseLong(id));
        em.remove(deleteHotel);
    }

    public void AddHotel(String txtName, String txtLocation, String txtHotelLvl, String txtLatitude, String txtLongtitude){
        MHotel addHotel = new MHotel();
        addHotel.setName(txtName);
        addHotel.setLocation(txtLocation);
        addHotel.setHotelLevel(txtHotelLvl);
        if (!txtLatitude.isEmpty()){
            addHotel.setLatitude(new BigDecimal(txtLatitude));
        }else{
            addHotel.setLatitude(null);
        }
        if (!txtLongtitude.isEmpty()){
            addHotel.setLongtitude(new BigDecimal(txtLongtitude));
        }else{
            addHotel.setLongtitude(null);
        }
        em.persist(addHotel);
    }

    public List<MHotel> SearchDataHotel(String txtKeyword) {
        try {
            String strQuery = "SELECT m FROM MHotel m ";
            String strWhere = " Where 1=1 ";
            strWhere += (!txtKeyword.isEmpty()) ? " AND CONCAT(m.name,' ',m.location,' ',m.hotelLevel) Like '%" + txtKeyword + "%' " : " ";
            strQuery += strWhere;
            return em.createQuery(strQuery).getResultList();
        } catch (Exception e) {
            return null;
        }
    }
}
